﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DesignPatterns.MediatorPatterns
{
    class MediatorPattern
    {
        void Main(string[] args)
        {
            ConcreteMediator cm = new ConcreteMediator();
            var c1 = new ConcreteColleague1(cm);
            var c2 = new ConcreteColleague2(cm);
            cm.setC1(c1);
            cm.setC2(c2);

            //调用同事方法之前，声明中介者，并分别注入同事类
            //同事之间互相协作的事情就丢给中介者完成，将原本复杂的蜘蛛网关系，变为以中介者为中心的星状关系，减少代码耦合

            //但是如果中介者的工作过于繁杂，反而会得不偿失，所以得考虑中介者拆分或不用中介者
            //mvc框架就是典型的中介者模式
            c1.selfMethod1();
            c1.depMethod1();

            c2.selfMethod2();
            c2.depMethod2();
        }
    }

    /// <summary>
    /// 抽象同事类
    /// </summary>
    public abstract class Colleague {
        protected Mediator mediator;
        public Colleague(Mediator _mediator) {
            this.mediator = _mediator;
        }
    }

    /// <summary>
    /// 同事1
    /// </summary>
    public class ConcreteColleague1 : Colleague
    {
        public ConcreteColleague1(Mediator _mediator) : base(_mediator) { }

        /// <summary>
        /// 自有方法
        /// </summary>
        public void selfMethod1() {
            //自己的业务逻辑
        }

        /// <summary>
        /// 依赖方法
        /// </summary>
        public void depMethod1() {
            //自己的业务逻辑

            //终结者处理的其他业务
            base.mediator.doSomething1();
        }
    }

    /// <summary>
    /// 同事2
    /// </summary>
    public class ConcreteColleague2 : Colleague
    {
        public ConcreteColleague2(Mediator _mediator) : base(_mediator) { }

        /// <summary>
        /// 自有方法
        /// </summary>
        public void selfMethod2()
        {
            //自己的业务逻辑
        }

        /// <summary>
        /// 依赖方法
        /// </summary>
        public void depMethod2()
        {
            //自己的业务逻辑

            //终结者处理的其他业务
            base.mediator.doSomething2();
        }
    }

    public abstract class Mediator {
        protected ConcreteColleague1 c1;
        protected ConcreteColleague2 c2;

        public ConcreteColleague1 getC1() {
            return c1;
        }

        public ConcreteColleague2 getC2()
        {
            return c2;
        }

        public void setC1(ConcreteColleague1 _c1)
        {
            c1= _c1;
        }

        public void setC2(ConcreteColleague2 _c2)
        {
            c2 = _c2;
        }
        public abstract void doSomething1();
        public abstract void doSomething2();

    }

    public class ConcreteMediator : Mediator
    {
        public override void doSomething1()
        {
            base.c1.selfMethod1();
            base.c2.selfMethod2();
        }

        public override void doSomething2()
        {
            base.c1.selfMethod1();
            base.c2.selfMethod2();
        }
    }
}
